package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Invoice;
import com.ericom.service.GenericManager;

public class InvoiceAction extends BaseAction {
	private GenericManager<Invoice, Long> invoiceManager;
	private List invoices;
	private Invoice invoice;
	private Long id;

	public Invoice getInvoice() {
		return invoice;
	}

	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setInvoiceManager(GenericManager<Invoice, Long> invoiceManager) {
		this.invoiceManager = invoiceManager;
	}

	public String list() {
		invoices = invoiceManager.getAll();
		return SUCCESS;
	}

	public List getInvoices() {
		return invoices;
	}

	public String delete() {
		invoiceManager.remove(invoice.getId());
		this.saveMessage(getText("invoice.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			invoice = invoiceManager.get(id);
		} else {
			invoice = new Invoice();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (invoice.getId() == null);
		invoice = invoiceManager.save(invoice);

		String key = (isNew) ? "invoice.added" : "invoice.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
